#include "mySort.h"
#include <iostream>
#include <string>

MySort::MySort()
{
num=0;
origin = NULL;
}

MySort::MySort(int n, int* arr)
{
	num=n;
	origin = new int[num];
	for(int i=0;i<num;i++)
	{
		origin[i]=arr[i];
	}
}

MySort::~MySort(){
	if(origin !=NULL)
	{
		delete[] origin;
	}
}

void MySort::changeSwap(int& a, int& b){
	int temp=a;
	a=b;
	b=temp;
}

string MySort::printNum(){
	string ss;
	for(int i=0;i<num;i++)
	{
		ss+=origin[i]+'0';
		ss+=" ";
	}
	ss+="\n";
	return ss;
}

string MySort::paritySort()
{
	 bool isSorted = false; // 标志位，记录是否已排好序
        string outputString; // 用于存储结果的字符串
        while (!isSorted) {
            isSorted = true;
            // 奇偶交换
            for (int i = 1; i < num - 1; i += 2) {
                if (origin[i] > origin[i + 1]) {
                    changeSwap(origin[i], origin[i + 1]);
                    isSorted = false;
                    outputString += printNum(); // 输出当前排序结果
                }
            }
            for (int i = 0; i < num - 1; i += 2) {
                if (origin[i] > origin[i + 1]) {
                    changeSwap(origin[i], origin[i + 1]);
                    isSorted = false;
                    outputString += printNum(); // 输出当前排序结果
                }
            }
        }
        return outputString;
}

string MySort::bubbleSort()
{
	bool isSorted = false; // 标志位，记录是否已排好序
        string outputString; // 用于存储结果的字符串
        int left = 0, right = num - 1; // 首尾指针
        while (!isSorted) {
            isSorted = true;
            // 从左到右冒泡
            for (int i = left; i < right; i++) {
                if (origin[i] > origin[i + 1]) {
                    changeSwap(origin[i], origin[i + 1]);
                    isSorted = false;
                    outputString += printNum(); // 输出当前排序结果
                }
            }
            right--;
            // 从右往左冒泡
            for (int i = right; i > left; i--) {
                if (origin[i] < origin[i - 1]) {
                    changeSwap(origin[i], origin[i - 1]);
                    isSorted = false;
                    outputString += printNum(); // 输出当前排序结果
                }
            }
            left++;
        }
        return outputString;
}